home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3676 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.0 KB  |  111 lines

  1. Path: gatekeeper.liffe.com!usenet
  2. From: ralph.mason@liffe.com (Ralph Mason)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [help] 3D Array class
  5. Date: Thu, 25 Jan 1996 14:23:19 GMT
  6. Organization: London International Financial Futures Exchange
  7. Message-ID: <4e8413$10u@gatekeeper.liffe.com>
  8. References: <4e5sg8$r4h@geraldo.cc.utexas.edu>
  9. NNTP-Posting-Host: ralph_pc.liffe.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. tian@utpapa.ph.utexas.edu (Shiyang Tian) wrote:
  13.  
  14. >Hi,
  15.  
  16. >I'd like to construct a 3D array class which can take negative indices. 
  17. >What's the best way to do it? A related question: is it possible to
  18.  
  19. That depends on ths size of the array and what you are going to do
  20. with it.
  21.  
  22. The simplest wat is to just have a regular array and subtracty an
  23. offset of any entries you make.
  24.  
  25.  
  26. >overload operator [][][] so that I can write something like following?
  27.  
  28. >class array3D { ... };
  29.  
  30. >array3D x;
  31. >array3D *y;
  32.  
  33. >y = new array3D(X, Y. Z);        // index for y is from -X to X, ... 
  34. >(*y)[1][2][-3] = x[-1][-2][3];
  35.  
  36. class int_a
  37. {
  38.     public:
  39.  
  40.     int* a;
  41.     int offset;
  42.     
  43.     int_a(int* pint,int off) { a=pint;offset=off; }
  44.  
  45.     int&  operator[](int index)
  46.     {
  47.         return *(a+index );
  48.     }
  49.  
  50. };
  51.  
  52. class array 
  53. {
  54.     private:    
  55.  
  56.     int * pData;
  57.     int xoff,yoff;
  58.     int xrange,yrange;
  59.     
  60.     public:
  61.  
  62.     array ( int xmin, int xmax ,int ymin,int ymax )
  63.         {
  64.         xrange =   xmax- xmin;
  65.         yrange =   ymax- ymin;
  66.                         
  67.         pData = (int*) new char [  sizeof(int) * (xrange + 1 )* (yrange + 1)
  68. ];
  69.  
  70.         xoff = xmin;
  71.         yoff = ymin;
  72.         };
  73.  
  74.     
  75.     int_a operator[](int indexx)
  76.     {
  77.         return int_a( pData+ ( yrange*( indexx-xoff ) ),yoff );
  78.     }
  79.  
  80.  
  81. };
  82.  
  83.  
  84.  
  85. int main()
  86. {
  87.     array ar(-5,2,3,7);
  88.  
  89.     ar[0][3]=1;
  90.     ar[-2][4]=7;
  91.     printf("%d,%d",ar[0][3],ar[-2][4]);
  92.  
  93.     return 0;
  94. }
  95.  
  96.  
  97. This only does 2d arrays but can easily be converted to a 3d array of
  98. greater.
  99.  
  100. Using a couple templates you should be able to extend this code to
  101. cover any sized arrays.
  102.  
  103. I hope this is of some help
  104.  
  105.  
  106. this rambling is from ralph.mason@liffe.com
  107. -----------------------------------------------------------------------
  108. University of Liffe, London UK                     My views are my own!
  109.  
  110.  
  111.